package com.mc.book.controller;

import com.mc.book.enums.ResponseEnum;
import com.mc.book.pojo.Result;
import com.mc.book.pojo.request.*;
import com.mc.book.service.BookService;
import com.mc.book.utils.UpLoadBookUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "书模块")
@RestController
public class BookApi extends BaseApi{
	private static final Logger log = LoggerFactory.getLogger(BookApi.class);
	@Autowired
	private BookService bookService;

	@PostMapping(value = "mc/book/addBookByAi")
	public Result addBookByAi(@RequestBody RequestUserUploadBook request)
	{

		if (StringUtils.isEmpty(request.getBook_name())) {
			return Result.fail(ResponseEnum.REQUEST_ENTRY_MISSING.getCode(),ResponseEnum.REQUEST_ENTRY_MISSING.getMessage());
		}

		RequestBook seBook = new RequestBook();
		seBook.setBook_name(request.getBook_name());
		List<RequestBook> books = bookService.selectBookList(seBook);
		if (books!=null&&books.size()>0) {
			return Result.fail(ResponseEnum.BOOK_HAVA.getCode(),ResponseEnum.BOOK_HAVA.getMessage());
		}

		//上传文件到后台
		String re = UpLoadBookUtil.upLoadBook(request.getBook_name());
		bookService.userUploadBook(request);

		return Result.success();
	}


	@ApiOperation("书查询")
	@PostMapping("mc/book/query")
	public Result bookQuery(@RequestBody RequestBook request){
		try {
			return bookService.getBookByName(request);
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}

	@ApiOperation("书详情查询")
	@PostMapping("mc/book/query/id")
	public Result bookQueryById(@RequestBody RequestBook request){
		try {
			return bookService.getBookById(request);
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}

	@ApiOperation("书目录查询")
	@PostMapping("mc/book/queryBookIndex")
	public Result queryBookIndex(@RequestBody RequestBookIndex request){
		try {
			return bookService.queryBookIndex(request);
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}

	@ApiOperation("书内容查询")
	@PostMapping("mc/book/queryBookContent")
	public Result queryBookContent(@RequestBody RequestBookContent request){
		try {
			return bookService.queryBookContent(request);
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}

	@ApiOperation("书标签列表")
	@PostMapping("mc/book/category")
	public Result bookCategory(){
		try {
			return bookService.bookCategory();
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}

	@ApiOperation("根据书标签获取书列表")
	@PostMapping("mc/book/getBookByCategory")
	public Result getBookByCategory(@RequestBody RequestBookCategory request){
		try {
			return bookService.getBookByCategory(request);
		}catch (Exception e){
			log.error("系统异常：", e);
			return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(),ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
		}
	}



}
